package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.ChessAraay;
import model.User;
import util.SocketUtil;
import util.StateCode;

public class ChessPanel extends JPanel implements Observer, ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer backIndex = 1;
	private JLabel[] Player = new JLabel[2];
	private String[] bt = { "开始", "悔棋", "和棋", "认输", "回放", "换肤" };
	private JButton[] jbt = new JButton[bt.length];
	private JLabel[] chessBox = new JLabel[2];

	private Image background;
	private GridPanel gridPanel;
	private ChessAraay chessAraay;

	public ChessPanel() {

		setLayout(new BorderLayout());
		setOpaque(false);

		chessAraay = new ChessAraay();
		gridPanel = new GridPanel(chessAraay);
		add(gridPanel);
		chessAraay.addObserver(this);

		JPanel top = new JPanel();
		JLabel jl = new JLabel("五子棋");
		top.add(jl);
		top.setOpaque(false);
		add(top, BorderLayout.NORTH);

		String[] str = { "玩家1", "玩家2" };
		JPanel[] wanjia = new JPanel[2];
		for (int i = 0; i < str.length; i++) {

			wanjia[i] = new JPanel();
			wanjia[i].setOpaque(false);
			wanjia[i].setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();
			JLabel image = new JLabel();
			image.setIcon(new ImageIcon(getClass().getResource("../images/p" + i + ".jpg")));
			Player[i] = new JLabel(str[i]);
			gbc.gridy = 0;
			gbc.insets = new Insets(0, 20, 0, 20);
			wanjia[i].add(image, gbc);
			
			gbc.gridy = 1;
			wanjia[i].add(Player[i], gbc);

			gbc.gridy = 3;
			chessBox[i] = new JLabel();
			chessBox[i].setPreferredSize(new Dimension(50, 37));
			wanjia[i].add(chessBox[i], gbc);

		}
		add(wanjia[0], BorderLayout.WEST);
		add(wanjia[1], BorderLayout.EAST);

		JPanel down = new JPanel();
		down.setOpaque(false);
		for (int i = 0; i < bt.length; i++) {
			jbt[i] = new JButton(bt[i]);
			jbt[i].addActionListener(this);
			down.add(jbt[i]);

		}
		add(down, BorderLayout.SOUTH);

	}

	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		background = new ImageIcon(getClass().getResource("../images/b" + backIndex + ".jpg")).getImage();
		g.drawImage(background, 0, 0, getWidth(), getHeight(), null);

	}

	@Override
	public void actionPerformed(ActionEvent e) {

		String command = e.getActionCommand();
		switch (command) {
		case "开始":
			SocketUtil.sendMessage(StateCode.QINGQIUKAISHI);
			gridPanel.getPrompt().setPrompt("等待对方同意", 48, Color.black, 1f);
			gridPanel.repaint();
			break;
		case "悔棋":
			SocketUtil.sendMessage(StateCode.QINGQIUHUIQI);
			break;
		case "认输":
			break;
		case "和棋":
			break;
		case "回放":
			chessAraay.replay(gridPanel);
			jbt[bt.length - 1].setEnabled(false);
			break;
		case "换肤":
			backIndex = new Random().nextInt(7);
			repaint();
			break;
		}
	}

	public void update(Observable o, Object arg) {
		if (arg instanceof User) {
			User user = (User) arg;
			if (o.equals(user))
				Player[1].setText(user.getName());
			else
				Player[0].setText(user.getName());
		} else if (arg instanceof Integer) {
			int code = (int) arg;
			switch (code) {
			case StateCode.QINGQIUKAISHI:
				int f = JOptionPane.showConfirmDialog(this, "对方请求开始，同意吗？", "提示信息", JOptionPane.YES_NO_OPTION);
				if (f == 0) {
					SocketUtil.sendMessage(StateCode.TONGYIKAISHI);
					enableButton();
					gridPanel.getPrompt().setMessage("");
					o.addObserver(gridPanel);
					chessAraay.setMy(false);
					setChessBox(false);
					chessAraay.clear();
				} else {
					SocketUtil.sendMessage(StateCode.JUJUEKAISHI);
				}
				break;
			case StateCode.TONGYIKAISHI:
				enableButton();
				gridPanel.getPrompt().setMessage("");
				o.addObserver(gridPanel);
				chessAraay.setMy(true);
				setChessBox(true);
				chessAraay.clear();
				break;

			case StateCode.JUJUEKAISHI:
				JOptionPane.showMessageDialog(this, "你被拒绝了！");
				gridPanel.getPrompt().setMessage("请点击开始");
				break;
			case StateCode.QINGQIUHUIQI:
				f = JOptionPane.showConfirmDialog(this, "对方请求悔棋，同意吗？", "提示信息", JOptionPane.YES_NO_OPTION);
				if (f == 0) {
					SocketUtil.sendMessage(StateCode.TONGYIHUIQI);
					chessAraay.regret();
				} else {
					SocketUtil.sendMessage(StateCode.JUJUEHUIQI);
				}
				break;
			case StateCode.TONGYIHUIQI:
				chessAraay.regret();
				break;
			
			  case StateCode.JUJUEHUIQI: JOptionPane.showMessageDialog(this,
			 "对方不同意"); break;
			 
			
			 case StateCode.HUIFANGJIESHU: jbt[bt.length - 1].setEnabled(true); break;
			 
			case StateCode.GAIBIANQIHE:
				if (chessAraay.isMy())
					setChessBox(false);
				else
					setChessBox(true);
				break;
			case StateCode.LIANJIECHONGZHI:
				chessAraay.clear();
				gridPanel.getPrompt().setPrompt("请点击开始", 48, Color.black, 1f);
				chessAraay.setMy(false);
				break;
			}
		} else if (arg instanceof Color) {
			Color color = (Color) arg;
			if (color.getRGB() == SocketUtil.getColor().getRGB()) {
				gridPanel.getPrompt().setPrompt("你赢了", 72, Color.green, 0.6f);
			} else {
				gridPanel.getPrompt().setPrompt("你输了", 72, Color.red, 0.6f);
			}
			 disableButton();
		}
		repaint();
	}

	private void setChessBox(boolean myself) {
		int i = 1, j = 0;
		ImageIcon black = new ImageIcon(getClass().getResource("../images/blackChess.png"));
		ImageIcon white = new ImageIcon(getClass().getResource("../images/whiteChess.png"));
		ImageIcon ii;
		if (myself) {
			i = 0;
			j = 1;
		}
		chessBox[i].setIcon(null);
		if (SocketUtil.getColor().getRGB() == Color.BLACK.getRGB()) {
			ii = black;
			if (j == 0)
				ii = white;
			chessBox[j].setIcon(ii);
		} else {
			ii = white;
			if (j == 0)
				ii = black;
			chessBox[j].setIcon(ii);
		}
	}

	private void disableButton() {
		jbt[0].setEnabled(true);
		jbt[1].setEnabled(false);
		jbt[2].setEnabled(false);
		jbt[3].setEnabled(false);
		jbt[4].setEnabled(true);
	}

	private void enableButton() {
		jbt[0].setEnabled(false);
		jbt[1].setEnabled(true);
		jbt[2].setEnabled(true);
		jbt[3].setEnabled(true);
		jbt[4].setEnabled(true);
	}
}
